package com.airnewslinkosky.ticket.ui;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.LinearLayout;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.calendar.CalendarView;
import com.airnewslinkosky.ticket.model.SearchFlight;
import com.airnewslinkosky.ticket.ui.flight.FlightQuery;
import com.airnewslinkosky.ticket.ui.flight.FlightRoundQuery;
import com.airnewslinkosky.ticket.util.DateUtil;
import com.mobclick.android.MobclickAgent;

public class CalendarActivity extends BaseActivity{
	private CalendarView calendarView;	
	private AlertDialog.Builder builder;
	private AlertDialog adMyDate;
	public static Activity activity;
	public  final int CALENDER_RESULT_OK = -100;
	public  static String searchFlightType;
	public  static SearchFlight searchFlight;
	private int mYear;
    private int mMonth;
    private int mDay;
    private String fromDateStr,toDateStr;
    public  final int DATE_DIALOG_ID = 11;
    public  final int TO_DATE_DIALOG_ID = 12;
    
    
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if (activity == null)
		{
			activity = this;
		}
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		
		LinearLayout mainLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.calendar, null);
		setContentView(mainLayout);
		calendarView = new CalendarView(this);
		CalendarView.searchFlight = searchFlight;
		mainLayout.addView(calendarView);
		
		//得到当前日期
        final Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		    case DATE_DIALOG_ID:
		    	return new DatePickerDialog(this, dsl, mYear, mMonth, mDay + 1);
		default:
			return null;
		}
	}

    //准备日期设置监听器
    private OnDateSetListener dsl = new DatePickerDialog.OnDateSetListener(){

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear =year;
			mMonth= monthOfYear;
			mDay= dayOfMonth;
			fromDateStr = mYear+"-"+DateUtil.pad(mMonth+1)+"-"+DateUtil.pad(mDay);
			
			System.out.println("mmm  fromDateStr="+fromDateStr);
//			Intent intent = getIntent();
//			Bundle bundle=new  Bundle();
//			bundle.putString("calendar", fromDateStr);
//			intent.putExtras(bundle);
//			setResult(CALENDER_RESULT_OK, intent);
			
			Intent intentR = new Intent();
			if(searchFlightType.equals("single")){
				intentR.setClass(CalendarActivity.this, FlightQuery.class);
				FlightQuery.calendarValue = fromDateStr;
				searchFlight.ftype = "single";
				FlightQuery.searchFlight = searchFlight;
			} 
			
			startActivity(intentR);
			
			finish();
		}
    };
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {  
		menu.add(0, 1, 0, "明天").setIcon(this.getResources().getDrawable(R.drawable.clock));  
	    menu.add(0, 2, 1, "指定日期").setIcon(this.getResources().getDrawable(R.drawable.calendar_small));   
		return true;  
	} 
	
	public boolean onOptionsItemSelected(MenuItem item) {  
		switch (item.getItemId()) {  
			case 1:  
				fromDateStr = mYear+"-"+DateUtil.pad(mMonth+1)+"-"+DateUtil.pad(mDay+1);
				Intent intentR = new Intent();
				System.out.println("onOptionsItemSelected fromDateStr =" + fromDateStr);
				System.out.println("onOptionsItemSelected searchFlightType =" + searchFlightType);
				System.out.println("onOptionsItemSelected searchFlightType =" + searchFlightType);
				
				String fromDate = getIntent().getStringExtra("fromDate");
				String toDate = getIntent().getStringExtra("toDate");
				
				System.out.println("onOptionsItemSelected fromDate =" + fromDate);
				System.out.println("onOptionsItemSelected toDate =" + toDate);
				
//				Intent intent = getIntent();
//				Bundle bundle=new  Bundle();
//				bundle.putString("calendar", fromDateStr);
//				intent.putExtras(bundle);
//				setResult(CALENDER_RESULT_OK, intent);
				
				if(searchFlightType.equals("single")){
					intentR.setClass(CalendarActivity.this, FlightQuery.class);
					FlightQuery.calendarValue = fromDateStr;
					searchFlight.ftype = "single";
					FlightQuery.searchFlight = searchFlight;
				} 
				startActivity(intentR);
				
				finish();
				return true;
			case 2:  
				showDialog(DATE_DIALOG_ID);
				return true;
		}  
		return false; 
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		calendarView.onKeyDown(keyCode, event);
		return super.onKeyDown(keyCode, event);
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
